<template>
    <div>
        <div class="PermissManaTool">
            <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>

            <el-input size="small" placeholder="请输入角色中文名" v-model="role.nameZh">
            </el-input>
            <el-button size="small" type="primary" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
        </div>
        <div class="permissManaMain">
            <!--折叠面板 手风琴模式 change自带事件会传:name的值
            通过 accordion 属性来设置是否以手风琴模式显示。-->
            <el-collapse     v-model="activeName" accordion @change="change">
                <el-collapse-item :title="r.nameZh" :name='r.id' v-for="(r,index) in roles" :key="index">
                    <!--卡片-->
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>

                            <!--删除按钮-->
                            <el-button  style="float: right; padding: 3px 0;color: red" type="text" icon="el-icon-delete"
                                @click="doDeleteRole(r)"
                            />
                        </div>
                        <div>
                            <!--树形控件 defaultProps展示子类和名字   node-key="id"事件通过id获取数据-->
                            <el-tree :data="allMenus"
                                     ref="tree"
                                     :key="index" :props="defaultProps"
                                     show-checkbox
                                     :default-checked-keys="selectedMenus"
                                     node-key="id"
                            ></el-tree>
                           <!-- 样式 容器布局 向右对齐-->
                            <div style="display: flex; justify-content: flex-end">
                                <el-button size="mini">取消修改</el-button>
                                <el-button size="mini" type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
                                <!--primary为主要按钮样式-->
                            </div>
                        </div>

                    </el-card>

                </el-collapse-item>
            </el-collapse>

        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data(){
            return{
                role:{
                    name:'',
                    nameZh:''
                },
                roles:[],
                allMenus:[],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                selectedMenus:[],
                activeName:-1
            }
        },
        mounted(){
            this.initRoles();
        },
        methods:{
            doAddRole(){
                if (this.role.name && this.role.nameZh){
                    this.postRequest('/system/basic/permiss/role',this.role).then(resp=>{
                       if (resp){
                           this.initRoles();
                           this.role.name='';
                           this.role.nameZh='';
                       }
                    });
                }else{
                    this.$message.error('所有字段不能为空！');
                }
            },
            doUpdate(rid,index){
              let tree = this.$refs.tree[index];
              let selectedKeys = tree.getCheckedKeys(true);/*传true只打印叶子节点*/
             let url='/system/basic/permiss/role?rid='+rid;
             let mids ='';
             selectedKeys.forEach(key=>{
                 mids += '&mids='+key;
             });
             url+=mids;


             this.putRequest(url).then(resp=>{
                 if (resp){
                     this.initRoles();
                     this.activeName=-1;
                 }
             });
            },
            doDeleteRole(role){
                this.$confirm('此操作将永久删除['+role.nameZh+'], 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/permiss/role/'+role.id).then(resp=>{
                        if (resp){
                            this.initRoles();
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },



            initSelectedMenus(rid){
              this.getRequest('/system/basic/permiss/mid/'+rid).then(resp=>{
                 if (resp){
                     this.selectedMenus=resp;
                 }
              });
            },
            change(rid){
               if (rid){
                  this.initAllMenus();
                  this.initSelectedMenus(rid);
                  this.shuaxin(rid);
               }
            },

            shuaxin(rid){
                this.initAllMenus();
                this.initSelectedMenus(rid);
            },
            initAllMenus(){
                this.getRequest('/system/basic/permiss/menus').then(resp=>{
                    if (resp){
                        this.allMenus=resp;
                    }
                });
            },
            initRoles(){
                this.getRequest('/system/basic/permiss/').then(resp=>{
                    if (resp){
                        this.roles=resp;
                    }
                });
            }
        }
    }
</script>

<style >
    .PermissManaTool{
        display: flex;/*使用容器布局 下面左对齐生效*/
        justify-content: flex-start;/*左对齐*/

    }
    .PermissManaTool .el-input{
        width: 300px;
        margin-right: 6px;
    }
    .permissManaMain{
        margin-top: 10px;
        width: 700px;
    }

</style>
